package com.workflow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

/**
 * NoBranchAndBackTest.java //
 * 不包含分支节点 并且回退的 测试类
 * @author yaojinshan
 * @since 2018年5月9日下午4:37:49
 */
public class NoBranchAndBackTest {
	private static Task task = null;
	private static List<TaskDetail> taskDetailList = null;
	private static Map<String,TaskDetail> taskDetaiMap = new HashMap<String, TaskDetail>();
	
	static{
		task = new Task("测试name", "测试Id", Task.STATUS_NORMAL);
		System.out.println("初始化工作流开始。。。。。");
		taskDetailList = new ArrayList<TaskDetail>();
		taskDetailList.add(initStartNode());
		taskDetailList.add(initMiddleNode());
		taskDetailList.add(initEndNode());
		System.out.println("初始化工作流完成，当前工作流共"+taskDetailList.size()+"个节点，内容为：" + taskDetailList.toString());
	}
	public static void main(String[] args) {
		List<TaskDetailHistory> historyList = new ArrayList<TaskDetailHistory>();
		int daiBanId = 0;
		String  operatorUser = "user";
		String startNodeId = "";
		TaskDetailHistory currentTaskDetailHistory = new TaskDetailHistory();
		for(TaskDetail td : taskDetailList){
			if(TaskDetail.TYPE_START.equals(td.getType())){
				currentTaskDetailHistory = copyTaskDetailToHistory(td);
				startNodeId = td.getId();
			}
		}
		String remark = "";
		//启动一个工作流
		DaiBan daiBan = new DaiBan(String.valueOf(daiBanId++), task.getId(), startNodeId, operatorUser);
		System.out.println("启动一个工作流，生成待办任务：" + daiBan.toString());
		int backTimes = 0;//回退次数
		String nextNodeId = "";//下一个节点Id
		//下一个节点Id 并不是最后一个节点Id
		while(!"-1".equals(nextNodeId)){
			if(TaskDetail.TYPE_END.equals(currentTaskDetailHistory.getType())){
				remark = "操作员：user操作了任务：" + currentTaskDetailHistory.getName() + ",当前节点为结束节点，流程结束。";
				currentTaskDetailHistory.setRemark(remark);
				currentTaskDetailHistory.setResult(TaskDetailHistory.result_true);
				historyList.add(currentTaskDetailHistory);
				break;
			}else{
				if(startNodeId.equals(currentTaskDetailHistory.getId()) || backTimes != 0){
					remark = "操作员：user操作了任务：" + currentTaskDetailHistory.getName() + ",进行下一个节点。";
					currentTaskDetailHistory.setResult(TaskDetailHistory.result_true);
					nextNodeId = currentTaskDetailHistory.getNextId();
				}else if(!startNodeId.equals(currentTaskDetailHistory.getId()) && backTimes == 0){
					//模拟回退一次
					remark = "操作员：user操作了任务：" + currentTaskDetailHistory.getName() + ",任务失败，回退到上一个节点。";
					currentTaskDetailHistory.setResult(TaskDetailHistory.result_fasle);
					nextNodeId = currentTaskDetailHistory.getBackId();
					backTimes++;
				}
				currentTaskDetailHistory.setRemark(remark);
				historyList.add(currentTaskDetailHistory);
				daiBan = new DaiBan(String.valueOf(daiBanId++), task.getId(),nextNodeId, operatorUser);
				currentTaskDetailHistory = copyTaskDetailToHistory(taskDetaiMap.get(nextNodeId));
			}
		}
		System.out.println("操作历史：" + historyList.toString());
	}
	/**
	 * 复制TaskDetail 到 history
	 */
	private static TaskDetailHistory copyTaskDetailToHistory(TaskDetail td) {
		JSON detailJson = (JSON) JSON.toJSON(td);
		return JSON.toJavaObject(detailJson, TaskDetailHistory.class);
	}
	private static TaskDetail initStartNode() {
		TaskDetail taskDetail = new TaskDetail();
		taskDetail.setId("0");
		taskDetail.setTaskId(task.getId());
		taskDetail.setName("测试任务start_node");
		taskDetail.setType(TaskDetail.TYPE_START);
		taskDetail.setNextId("1");
		taskDetaiMap.put(taskDetail.getId(), taskDetail);
		return taskDetail;
	}
	private static TaskDetail initMiddleNode() {
		TaskDetail taskDetail = new TaskDetail();
		taskDetail.setId("1");
		taskDetail.setTaskId(task.getId());
		taskDetail.setName("测试任务middle_node");
		taskDetail.setType(TaskDetail.TYPE_MIDDLE);
		taskDetail.setNextId("2");
		taskDetail.setBackId("0");
		taskDetaiMap.put(taskDetail.getId(), taskDetail);
		return taskDetail;
	}
	private static TaskDetail initEndNode() {
		TaskDetail taskDetail = new TaskDetail();
		taskDetail.setId("2");
		taskDetail.setTaskId(task.getId());
		taskDetail.setName("测试任务end_node");
		taskDetail.setType(TaskDetail.TYPE_END);
		taskDetail.setNextId("-1");
		taskDetail.setBackId("1");
		taskDetaiMap.put(taskDetail.getId(), taskDetail);
		return taskDetail;
	}

}
